package com.lei.baweiliao.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.SPUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lei.baweiliao.R;
import com.lei.baweiliao.threadhelper.ThreadHelper;
import com.lei.baweiliao.utils.SpUtils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText login_username;
    private EditText login_pwd;
    private Button login_bt;
    private Button login_reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean login = SPUtils.getInstance().getBoolean("login");
        if (login==true){
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        login_username = (EditText) findViewById(R.id.login_username);
        login_pwd = (EditText) findViewById(R.id.login_pwd);
        login_bt = (Button) findViewById(R.id.login_bt);
        login_reg = (Button) findViewById(R.id.login_reg);

        login_bt.setOnClickListener(this);
        login_reg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_bt:
                submit();
                break;
            case R.id.login_reg:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                finish();
                break;
        }
    }

    private void submit() {
        // validate
        String username = login_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入要登录的用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = login_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "请输入要登录的密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        ThreadHelper.getInstance().getExecutors().submit(() -> EMClient.getInstance().login(username,pwd,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                runOnUiThread(() -> Toast.makeText(LoginActivity.this, "登录聊天服务器成功", Toast.LENGTH_SHORT).show());
                SPUtils.getInstance().put("login",true);
                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                finish();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！");
                runOnUiThread(() -> Toast.makeText(LoginActivity.this, message+"", Toast.LENGTH_SHORT).show());
            }
        }));

    }
}
